package dao.hibernate;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Date;
import java.util.List;

import beans.Bill;
import dao.IBillDAO;
import dao.hibernate.GenericDAOImpl;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Projections;

/**
 * Bill持久化实现，基于hibernate
 * @see dao.ICustomerDAO
 */
public class BillDAOImpl extends GenericDAOImpl<Bill, Integer> implements IBillDAO {

    @SuppressWarnings("unchecked")
    public List<Bill> findByCreateDateSpacingPerPage(Date startDate,
            Date endDate, int start, int limit) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Bill.class);
        criteria.add(Expression.between("ctime", startDate, endDate));
        return getHibernateTemplate().findByCriteria(criteria,start,limit);
    }

    public int findByCreateDateSpacingCount(Date startDate,Date endDate) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Bill.class);
        criteria.add(Expression.between("ctime", startDate, endDate));
        criteria.setProjection(Projections.rowCount());
        return (Integer)getHibernateTemplate().findByCriteria(criteria).get(0);
    }

//	@Override
//	public void addBill(vo.Bill bill) throws Exception {
//		try {
//			//this.bills.add(bill);
//			this.save(bill)
//		}
//		catch(Exception e){
//			throw e;
//		}
//		finally{
//		}
//	}

	@Override
	public Bill getBill(String uid) throws Exception {
		DetachedCriteria criteria = DetachedCriteria.forClass(Bill.class);
        criteria.add(Expression.eq("id", uid));
        return (Bill)(getHibernateTemplate().findByCriteria(criteria)).get(0);
	}

	//@Override
	//public List listAllBill() throws Exception {
	//	DetachedCriteria criteria = DetachedCriteria.forClass(Bill.class);
    //    return getHibernateTemplate().findByCriteria(criteria);
	//}
	
	@SuppressWarnings("unchecked")
	public List<Bill> findAllBill() {
    	DetachedCriteria criteria = DetachedCriteria.forClass(Bill.class);
    	return getHibernateTemplate().findByCriteria(criteria);
    }

}
